package com.hsytech.dijiebao.account;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.hsytech.dijiebao.MainActivity;
import com.hsytech.dijiebao.R;
import com.hsytech.dijiebao.base.BaseActivity;
import com.hsytech.dijiebao.bean.TokenBean;
import com.hsytech.dijiebao.utils.Constants;
import com.hsytech.dijiebao.utils.HttpUtil;
import com.hsytech.dijiebao.utils.SharedPreferencesTools;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

public class SignInActivity extends BaseActivity {
    private ImageButton top_back_btn;
    private Button bar_right_top_btn;
    private TextView bar_title_tv, intent_sign_up, find_password;
    private EditText password, email;
    private LinearLayout finish, top_bak_liner;
    private DPHttpUtil.HttpCallback callBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            TokenBean tb = com.alibaba.fastjson.JSONObject.parseObject(response,
                    TokenBean.class);
            Log.d("mylog", tb.getRytoken());
            Log.d("mylog", tb.getId() + "=uiduid");
            Log.d("mylog", tb.getToken());

            SharedPreferencesTools.writeRongToken(SignInActivity.this, tb.getRytoken());
            SharedPreferencesTools.writeToken(SignInActivity.this, tb.getToken());
            SharedPreferencesTools.writeUid(SignInActivity.this, tb.getId());
            SharedPreferencesTools.writeEmail(SignInActivity.this, email.getText().toString());
            startActivityWithTaskCleared(MainActivity.class);
        }

        @Override
        public void onError(int what, int type, VolleyError error) {

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);

        initView();
    }

    private boolean checkBeforePost() {

        if (email.getText().toString().equals("")) {
            showToast("请输入注册邮箱");
            return false;
        } else if (password.getText().toString().equals("")) {
            showToast("请输入密码");
            return false;
        }
        return true;
    }

    private void initView() {
        initTitle();
        initBody();
    }

    private void initBody() {
        password = (EditText) findViewById(R.id.password);
        email = (EditText) findViewById(R.id.email);
        finish = (LinearLayout) findViewById(R.id.finish);

        if (TextUtils.isEmpty(SharedPreferencesTools.getEmail(SignInActivity.this))) {

        } else {
            email.setText(SharedPreferencesTools.getEmail(SignInActivity.this));

        }

        intent_sign_up = (TextView) findViewById(R.id.intent_sign_up);
        find_password = (TextView) findViewById(R.id.find_password);

        find_password.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(SignInActivity.this, ForgetPasswordActivity.class);
                startActivity(intent);
            }
        });
        intent_sign_up.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(SignInActivity.this, SignUpActivity.class);
                startActivity(intent);
            }
        });
        finish.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (checkBeforePost()) {
                    postHttp();
                    // finish();
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            startActivityWithTaskCleared(MainActivity.class);
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initTitle() {
        top_back_btn = (ImageButton) findViewById(R.id.top_back_btn);
        top_back_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                startActivityWithTaskCleared(MainActivity.class);

            }
        });
        bar_right_top_btn = (Button) findViewById(R.id.bar_right_top_btn);
        bar_right_top_btn.setVisibility(View.GONE);
        bar_title_tv = (TextView) findViewById(R.id.bar_title_tv);
        bar_title_tv.setText("登录");
        top_bak_liner = (LinearLayout) findViewById(R.id.top_bak_liner);
        top_bak_liner.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivityWithTaskCleared(MainActivity.class);

            }
        });
    }

    private void postHttp() {

        Map<String, String> map = new HashMap<>();
        map.put("email", email.getText().toString());
        map.put("password", password.getText().toString());
        HttpUtil.httpPostWithParams(this, 0, Constants.HTTP_SIGN_IN, map, callBack);

    }

}
